package com.zls.service.impl;

import com.zls.mapper.BranchMapper;
import com.zls.page.PageResult;
import com.zls.page.QueryPageBean;
import com.zls.pojo.Branch;
import com.zls.service.BranchService;
import com.zls.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class BranchServiceImpl implements BranchService {
    @Autowired
    private BranchMapper branchMapper;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = new PageResult();
        Integer currentPage = queryPageBean.getCurrentPage();//当前页
        Integer pageSize = queryPageBean.getPageSize();//每页大小
        String queryString = queryPageBean.getQueryString();//查询条件
        //total总记录数
        List<Branch> page = branchMapper.findBra((currentPage - 1) * pageSize, pageSize,queryString);
        //rows  当前页的数据
        Long count = branchMapper.count(queryString);
        pageResult.setRows(page);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public List<Branch> findAll() {
        return branchMapper.findAll();
    }

    @Override
    @Transactional
    public int addBranch(Branch branch) {
        branch.setUpdatetime(new Date());
        branch.setBid(UUIDUtils.uuid());
        int i = branchMapper.addbranch(branch);
        return i;
    }

    @Override
    @Transactional
    public Branch findByBid(String bid) {
        return branchMapper.findByBid(bid);
    }

    @Override
    @Transactional
    public int editBranch(Branch branch) {
        branch.setUpdatetime(new Date());
        int i = branchMapper.updatebranch(branch);
        return i;
    }

    @Override
    @Transactional
    public int deleteBranch(String bid) {
        int i = branchMapper.deletebranch(bid);
        return i;
    }

    @Override
    public List<Branch> findByPid(int pid) {
        return branchMapper.findByPid(pid);
    }
}
